package message

import (
	"encoding/json"
	"go-study-history/tank_war/clinet"
	"log"
	"time"
)

/*
发送消息
*/
func SendMessage(msg interface{}) {
	for {
		ws := clinet.WsClient.Client
		if ws == nil {
			ws = clinet.Connect("发送消息未创建客户端,启动连接")
		}
		body, _ := json.Marshal(msg)
		_, err := ws.Write(body)
		if err != nil {
			clinet.WsClient.Client = nil
			log.Printf("发送消息中监听断线，客户端初始化为nil,\n错误内容:%s\n", err)
			continue
		}
		break
	}

}

/**
发送心跳消息
*/
func SendHeart() {
	for {
		SendMessage(`{"type":0x00}`)
		time.Sleep(time.Second * 30)
	}
}
